{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymysql #导入模块\n",
    "import pandas as pd\n",
    "\n",
    "#远程连接数据库\n",
    "db = pymysql.connect(\n",
    "         host='10.102.52.248',\n",
    "         port=3306,\n",
    "         user='root',\n",
    "         passwd='root',\n",
    "         db='casepro',\n",
    "         charset='utf8'\n",
    "         )\n",
    "def link_mysql(db):\n",
    "    #使用cursor()方法获取操作游标 \n",
    "    cursor = db.cursor()\n",
    "    sql = \"\"\"SELECT * FROM `t_plane_order`\"\"\"\n",
    "    sql1 = \"\"\"SELECT * FROM `t_plane_weather`\"\"\"\n",
    "    try:\n",
    "        cursor.execute(sql)  # 执行sql语句\n",
    "        result = cursor.fetchall()\n",
    "        columns = ['航班时刻表','航班号','子订单','日期','头等舱','公务舱','经济舱','其他','头等舱总数','公务舱总数','经济舱总数','其他总数']\n",
    "        frame = pd.DataFrame(list(result),columns=columns)\n",
    "        frame.to_csv(\"航班天气因素上座情况预测分析案例.csv\",encoding='utf-8')\n",
    "\n",
    "        cursor.execute(sql1)  # 执行sql语句\n",
    "        result1 = cursor.fetchall()\n",
    "        columns_weather = ['日期','高温','低温','天气状况','风','空气']\n",
    "        frame_weather = pd.DataFrame(list(result1),columns=columns_weather)\n",
    "        frame_weather.to_csv(\"天气数据.csv\",encoding='utf-8')\n",
    "    except Exception:\n",
    "        db.rollback()  # 发生错误时回滚\n",
    "        print(\"查询失败\")\n",
    "\n",
    "    cursor.close()  # 关闭游标\n",
    "    db.close()  # 关闭数据库连接\n",
    "link_mysql(db)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Anaconda\\lib\\site-packages\\IPython\\core\\interactiveshell.py:3249: DtypeWarning: Columns (3,5) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  if (await self.run_code(code, result,  async_=asy)):\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "各字段的空占比为：\n",
      "子订单      0.272959\n",
      "航班号      0.013529\n",
      "日期       0.000389\n",
      "其他总数     0.000000\n",
      "经济舱总数    0.000000\n",
      "公务舱总数    0.000000\n",
      "头等舱总数    0.000000\n",
      "其他       0.000000\n",
      "经济舱      0.000000\n",
      "公务舱      0.000000\n",
      "头等舱      0.000000\n",
      "航班时刻表    0.000000\n",
      "dtype: float64\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:34: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>航班时刻表</th>\n",
       "      <th>子订单</th>\n",
       "      <th>头等舱</th>\n",
       "      <th>公务舱</th>\n",
       "      <th>经济舱</th>\n",
       "      <th>其他</th>\n",
       "      <th>头等舱总数</th>\n",
       "      <th>公务舱总数</th>\n",
       "      <th>经济舱总数</th>\n",
       "      <th>其他总数</th>\n",
       "      <th>高温</th>\n",
       "      <th>低温</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>航班号</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>1487</td>\n",
       "      <td>4.658364e+08</td>\n",
       "      <td>1209.0</td>\n",
       "      <td>911.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>38468.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8536.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>128040.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>20745.0</td>\n",
       "      <td>8746.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1515D</td>\n",
       "      <td>1.508307e+06</td>\n",
       "      <td>15.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>202.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>150.0</td>\n",
       "      <td>1035.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>102.0</td>\n",
       "      <td>48.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>160R</td>\n",
       "      <td>1.117257e+06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>158.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>64.0</td>\n",
       "      <td>42.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4678</td>\n",
       "      <td>7.610336e+08</td>\n",
       "      <td>1435.0</td>\n",
       "      <td>564.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>31895.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7572.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>139578.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>24851.0</td>\n",
       "      <td>13711.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4678D</td>\n",
       "      <td>3.175630e+06</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>112.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>320.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>128.0</td>\n",
       "      <td>48.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>ZH9889A</td>\n",
       "      <td>1.568665e+06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>141.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>159.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-28.0</td>\n",
       "      <td>-36.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>ZH9889D</td>\n",
       "      <td>2.509918e+07</td>\n",
       "      <td>37.0</td>\n",
       "      <td>43.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1347.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>224.0</td>\n",
       "      <td>4460.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>701.0</td>\n",
       "      <td>393.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>ZH9970</td>\n",
       "      <td>1.107735e+09</td>\n",
       "      <td>2914.0</td>\n",
       "      <td>1804.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>37449.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7312.0</td>\n",
       "      <td>1632.0</td>\n",
       "      <td>167249.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>30884.0</td>\n",
       "      <td>15132.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>ZH9970D</td>\n",
       "      <td>1.571573e+07</td>\n",
       "      <td>31.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>703.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>2166.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>469.0</td>\n",
       "      <td>219.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>ZH997Z</td>\n",
       "      <td>6.286404e+06</td>\n",
       "      <td>8.0</td>\n",
       "      <td>22.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>416.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>888.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>260.0</td>\n",
       "      <td>170.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2017 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                航班时刻表     子订单     头等舱  公务舱      经济舱   其他   头等舱总数   公务舱总数  \\\n",
       "航班号                                                                        \n",
       "1487     4.658364e+08  1209.0   911.0  0.0  38468.0  0.0  8536.0     0.0   \n",
       "1515D    1.508307e+06    15.0    26.0  0.0    202.0  4.0     0.0   150.0   \n",
       "160R     1.117257e+06     1.0     0.0  0.0      0.0  0.0     8.0     0.0   \n",
       "4678     7.610336e+08  1435.0   564.0  2.0  31895.0  0.0  7572.0     0.0   \n",
       "4678D    3.175630e+06     6.0     1.0  0.0    112.0  0.0    16.0     0.0   \n",
       "...               ...     ...     ...  ...      ...  ...     ...     ...   \n",
       "ZH9889A  1.568665e+06     1.0     2.0  0.0    141.0  0.0     0.0     8.0   \n",
       "ZH9889D  2.509918e+07    37.0    43.0  0.0   1347.0  0.0     0.0   224.0   \n",
       "ZH9970   1.107735e+09  2914.0  1804.0  0.0  37449.0  0.0  7312.0  1632.0   \n",
       "ZH9970D  1.571573e+07    31.0    23.0  0.0    703.0  0.0    56.0    56.0   \n",
       "ZH997Z   6.286404e+06     8.0    22.0  0.0    416.0  0.0    48.0     0.0   \n",
       "\n",
       "            经济舱总数  其他总数       高温       低温  \n",
       "航班号                                        \n",
       "1487     128040.0   0.0  20745.0   8746.0  \n",
       "1515D      1035.0   0.0    102.0     48.0  \n",
       "160R        158.0   0.0     64.0     42.0  \n",
       "4678     139578.0   0.0  24851.0  13711.0  \n",
       "4678D       320.0   0.0    128.0     48.0  \n",
       "...           ...   ...      ...      ...  \n",
       "ZH9889A     159.0   0.0    -28.0    -36.0  \n",
       "ZH9889D    4460.0   0.0    701.0    393.0  \n",
       "ZH9970   167249.0   0.0  30884.0  15132.0  \n",
       "ZH9970D    2166.0   0.0    469.0    219.0  \n",
       "ZH997Z      888.0   0.0    260.0    170.0  \n",
       "\n",
       "[2017 rows x 12 columns]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "#对航班数据进行清理\n",
    "def yucli_plane():\n",
    "    df_plane = pd.read_csv('航班天气因素上座情况预测分析案例.csv',encoding='utf-8')\n",
    "    df_plane = pd.DataFrame(data=df_plane)\n",
    "    df_plane = df_plane.drop(columns='Unnamed: 0')\n",
    "    #判断其空占比\n",
    "    df_plane_percent = df_plane.isna().sum().sort_values(ascending=False) / len(df_plane)\n",
    "    print(\"各字段的空占比为：\\n{}\".format(df_plane_percent))\n",
    "\n",
    "    #将子订单的非数字改为0\n",
    "    df_plane.iloc[:,2] = np.where(df_plane.iloc[:,2].str.isdigit() == False,'非数字',df_plane.iloc[:,2])\n",
    "    df_plane = df_plane[df_plane.iloc[:,2] != '非数字']\n",
    "\n",
    "    #将子订单的空值替换为0\n",
    "    df_plane.iloc[:,2].fillna('0.0', inplace = True)\n",
    "    df_plane.iloc[:,2] = df_plane.iloc[:,2].astype(float)\n",
    "\n",
    "    #将不规范的航班号删除\n",
    "    df_plane.iloc[:,1] = np.where(df_plane.iloc[:,1].str.isalnum() !=  True,'不规范',df_plane.iloc[:,1])\n",
    "    df_plane = df_plane[df_plane.iloc[:,1] != '不规范']\n",
    "    \n",
    "    #删除空数据\n",
    "    df_plane_notNu = df_plane.dropna(axis=0)\n",
    "    #删除重复数据\n",
    "    df_plane_notNu = df_plane_notNu.drop_duplicates()\n",
    "    #将str类型改为float\n",
    "    for i in range(4,12):\n",
    "        df_plane_notNu.iloc[:,i] = df_plane_notNu.iloc[:,i].astype(float)\n",
    "    #将负值替换成零\n",
    "    for i in range(4,12):\n",
    "        df_plane_notNu.iloc[:,i][df_plane_notNu.iloc[:,i] < 0] = 0.0\n",
    "    #删除日期中不规范的数据\n",
    "    df_plane_notNu[df_plane_notNu.iloc[:,3] == \"0.0\"] = np.nan\n",
    "    df_plane_notNu.dropna(axis=0)\n",
    "    return df_plane_notNu\n",
    "\n",
    "#对天气数据进行处理\n",
    "def yucli_weath():\n",
    "    df_weath = pd.read_csv('天气数据.csv',encoding='utf-8')\n",
    "    df_weath = pd.DataFrame(df_weath)\n",
    "    #删除无分析价值数据\n",
    "    df_weath = df_weath.drop('Unnamed: 0',axis=1)\n",
    "    #将日期和空气格式化\n",
    "    df_weath[\"日期\"] = df_weath[\"日期\"].apply(lambda x:str(x)[0:10])\n",
    "    df_weath['空气'] = df_weath['空气'].apply(lambda x:str(x)[:-1])\n",
    "    return df_weath\n",
    "df_result = pd.merge(left=yucli_plane(), right=yucli_weath(), on=\"日期\", how=\"inner\")\n",
    "#df_result.groupby(['日期','航班号','天气状况','风','空气']).sum()\n",
    "df_result.groupby('航班号').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'show_air_rain' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-560de00df4d1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mshow_air_rain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf_result\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m \u001b[0mshow_air_lv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf_result\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;31m#根据风统计航班各舱的情况\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'show_air_rain' is not defined"
     ]
    }
   ],
   "source": [
    "#根据空气统计航班的上座率\n",
    "\n",
    "\n",
    "show_air_rain(df_result)\n",
    "show_air_lv(df_result)\n",
    "#根据风统计航班各舱的情况\n",
    "show_win_rain(df_result)\n",
    "show_win_lv(df_result)\n",
    "#根据低温统计航班各舱的情况\n",
    "show_weath_height(df_result)\n",
    "show_weath_low(df_result)\n",
    "\n",
    "#根据温度统计航班的上座率\n",
    "show_weath_lv(df_result)\n",
    "show_weather_rain(df_result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def yc(df_result):\n",
    "    df_result = df_result.replace(['中雨~小雨','多云~中雨','多云~小雨','多云~晴','多云~阴','多云~雷阵雨','多云~霾','大暴雨~中雨','大雨~多云','小雨~中雨','小雨~多云','小雨~大雨','小雨~小到中雨','小雨~晴','小雨~阴','小雪~多云','小雪~阴','晴~多云','晴~小雨','晴~阴','晴~霾','阴~多云','阴~小雨','阴~晴','阴~阵雨','阴~雷阵雨','阵雨~多云','阵雨~阴','雨夹雪~大雪','雷阵雨~阵雨','霾~多云','霾~小雨','霾~晴','霾~阴','霾~阵雨','霾~雨夹雪','霾~雷阵雨','霾~雾'],['小雨','中雨','小雨','晴','阴','雷阵雨','霾','中雨','中雨','小雨','小雨','中雨','小雨','晴','阴','多云','阴','晴','晴','阴','霾','阴','小雨','阴','阴','雷阵雨','阵雨','阵雨','大雪','雷阵雨','多云','小雨','晴','阴','阵雨','雨夹雪','雷阵雨','雾'])\n",
    "    df_result['上座人数']=df_result[['公务舱','头等舱','经济舱','其他']].sum(axis=1)\n",
    "    df_result['总座位数']=df_result[['头等舱总数','公务舱总数','经济舱总数','其他总数']].sum(axis=1)\n",
    "    y1=(df_result['上座人数']/df_result['总座位数'])\n",
    "    df_result['上座率'] = y1\n",
    "    df_result = df_result[df_result.iloc[:,19] != 0.0]\n",
    "    df_result.iloc[:,19] = np.where(df_result.iloc[:,19] == np.inf,'错误数据',df_result.iloc[:,19])\n",
    "    df_result = df_result[df_result.iloc[:,19] != '错误数据']\n",
    "    df_result = df_result.dropna(axis=0)\n",
    "    df_result\n",
    "    data = pd.concat([df_result['天气状况'],df_result['上座率']],axis=1)\n",
    "    data.to_csv('根据天气状况特征预测.csv')\n",
    "yc(df_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        Unnamed: 0 天气状况  上座率\n",
      "0                0   多云  NaN\n",
      "1                1   多云  NaN\n",
      "2                2   多云  NaN\n",
      "3                5   多云  NaN\n",
      "4                6   多云  NaN\n",
      "...            ...  ...  ...\n",
      "523667      922778    晴  NaN\n",
      "523668      922779    晴  NaN\n",
      "523669      922780    晴  NaN\n",
      "523670      922781    晴  NaN\n",
      "523671      922782    晴  NaN\n",
      "\n",
      "[523672 rows x 3 columns]\n"
     ]
    }
   ],
   "source": [
    "data=pd.read_csv(\"根据天气状况特征预测.csv\")\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0.09686981956931828\n",
      "岭回归的权重系数： [ 0.0018115   0.00153113  0.00434221 -0.00210162  0.          0.00395489\n",
      " -0.00093869 -0.00255945  0.00351689  0.00069137  0.00394667  0.\n",
      " -0.00219024]\n",
      "岭回归的偏置： 0.3782894736842105\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import SplineTransformer\n",
    "#from sklearn.decomposition import PCA\n",
    "from sklearn.linear_model import Ridge\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn import linear_model\n",
    "import numpy\n",
    "#取数据\n",
    "df=pd.read_csv(\"F://根据天气状况特征预测.csv\")\n",
    "#处理数据\n",
    "data=df.iloc[:,0:3]\n",
    "#print(data[\"天气状况\"])\n",
    "#print(\"原数据：\",data)\n",
    "data=pd.get_dummies(data,columns=['天气状况'])\n",
    "#print(\"哑变量之后的数据：\",data)\n",
    "#划分数据集\n",
    "x_train,x_test,y_train,y_test=train_test_split(data,df[\"上座率\"],test_size=0.3)\n",
    "#print(\"测试集\",x_test)\n",
    "#print(y_test)\n",
    "#print(data)\n",
    "transfer=StandardScaler()\n",
    "x_train=transfer.fit_transform(x_train)\n",
    "x_test=transfer.transform(x_test)\n",
    "#print(\"标准化之后的测试集\",x_test)\n",
    "#创建模型：\n",
    "#预估器：岭回归\n",
    "estimator=Ridge(alpha=1.0,max_iter=10000,solver=\"auto\")\n",
    "mode=estimator.fit(x_train,y_train)\n",
    "pre_test=estimator.predict(x_test)\n",
    "print(estimator.score(x_test,y_test))\n",
    "print(\"岭回归的权重系数：\",estimator.coef_)\n",
    "print(\"岭回归的偏置：\",estimator.intercept_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "岭回归的权重系数： [ 0.0136505  -0.000845    0.00749202  0.00675541  0.00222597  0.00713709\n",
      " -0.00124024 -0.00820292 -0.00309643 -0.00243195  0.00096645 -0.00047499\n",
      " -0.00783493]\n",
      "岭回归的偏置： 0.37666666666666665\n"
     ]
    }
   ],
   "source": [
    "#预估器：岭回归\n",
    "estimator=Ridge()\n",
    "mode=estimator.fit(x_train,y_train)\n",
    "pre_test=estimator.predict(x_test)\n",
    "print(\"岭回归的权重系数：\",estimator.coef_)\n",
    "print(\"岭回归的偏置：\",estimator.intercept_)\n",
    "#estimator.score(y_test,pre_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'天气状况'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32md:\\Anaconda\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mget_loc\u001b[1;34m(self, key, method, tolerance)\u001b[0m\n\u001b[0;32m   2896\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2897\u001b[1;33m                 \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   2898\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: '天气状况'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-52-ed2d5f37ac04>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"天气状况\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"天气状况\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreplace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mx_train\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mx_test\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my_train\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my_test\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtrain_test_split\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"天气状况_中雨\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"天气状况_霾\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"上座率\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;31m#print(x_test)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;31m#print(y_test)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;31m#print(data)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Anaconda\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   2978\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnlevels\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2979\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getitem_multilevel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2980\u001b[1;33m             \u001b[0mindexer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   2981\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mis_integer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2982\u001b[0m                 \u001b[0mindexer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Anaconda\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mget_loc\u001b[1;34m(self, key, method, tolerance)\u001b[0m\n\u001b[0;32m   2897\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2898\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2899\u001b[1;33m                 \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_maybe_cast_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   2900\u001b[0m         \u001b[0mindexer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtolerance\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtolerance\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2901\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mindexer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mindexer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msize\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: '天气状况'"
     ]
    }
   ],
   "source": [
    "data[\"天气状况\"]=data[\"天气状况\"].str.replace(\"\")\n",
    "x_train,x_test,y_train,y_test=train_test_split(data.loc[:,\"天气状况_中雨\":\"天气状况_霾\"],data[\"上座率\"])\n",
    "#print(x_test)\n",
    "#print(y_test)\n",
    "#print(data)\n",
    "transfer=StandardScaler()\n",
    "x_train=transfer.fit_transform(x_train)\n",
    "x_test=transfer.transform(x_test)\n",
    "print(x_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
